const { log } = require('node:console')
const http = require('node:http')
const url = require('node:url')

/**
 * req: 获取前端传参
 * res: 返回前端数据
 */
const server = http.createServer((req, res) => {
    const { pathname, query } = url.parse(req.url, true)
    if (req.method.toLocaleLowerCase() === 'post') {
        if (pathname === '/login') {
            let data = ''
            req.on('data', (chunk) => {
                data += chunk
            })
            req.on('end', () => {
                console.log(data)
                res.statusCode = 200
                res.setHeader('content-type', 'application/json')
                res.end(`post 登录${data}`)
            })

        } else {
            res.statusCode = 404
            res.end('404')
        }

    } else if (req.method.toLocaleLowerCase() === 'get') {
        console.log(query)
        if (pathname === '/list') {
            res.statusCode = 200
            res.setHeader('content-type', 'application/json')
            res.end('get qingqiu')
        } else {
            res.statusCode = 404
            res.end('404')
        }
    }
})

server.listen(98, () => {
    console.log('98端口号启动成功')
})